﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Cole.Entities;
using Cole.Service;

namespace Cole.GUI
{
    public partial class BuscarAlumno : Form
    {
        private IAlumnoBl alumnoBl;
        public BuscarAlumno()
        {
            InitializeComponent();
            if (alumnoBl == null)
            {
                alumnoBl = new AlumnoBl();
            }
        }

        private void BuscarAlumno_Load(object sender, EventArgs e)
        {
            alumnoBindingSource.DataSource = alumnoBl.GetAlumnoByApellidosNombresDNI("%");
        }
        private void loadData()
        {
            if (txtCriterio.Text == "")
            {
                alumnoBindingSource.DataSource = alumnoBl.GetAlumnoByApellidosNombresDNI("%");
            }
            else
            {
                alumnoBindingSource.DataSource = alumnoBl.GetAlumnoByApellidosNombresDNI(txtCriterio.Text);

            }
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            loadData();
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmNuevo = new RegistrarAlumno();
            frmNuevo.nuevo();
            frmNuevo.ShowDialog();
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            try
            {
                var alumno = alumnoBindingSource.Current as Alumno;
                if (alumno != null)
                {
                    var rpt = MessageBox.Show(this, "Desea eliminar el Alumno: " + alumno.nombres + " " + alumno.apellidos, "Mensaje",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question);
                    if (rpt == DialogResult.Yes)
                    {
                        alumnoBindingSource.RemoveCurrent();
                        alumnoBl.EliminarAlumno(alumno.Id);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            var alumno = alumnoBindingSource.Current as Alumno;
            if (alumno != null)
            {
                var frmModificar = new ModificarAlumno();
                frmModificar.Recibir(alumno);
                frmModificar.ShowDialog();

            }
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            loadData();
        }
    }
}
